package com.epam.weblibrary.controllers;

import com.epam.weblibrary.entities.user.Roles;
import com.epam.weblibrary.entities.user.User;
import com.epam.weblibrary.service.user.UserService;
import com.epam.weblibrary.service.user.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created by Iurii_Miedviediev on 9/10/2014.
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView getRegisterPage(@RequestParam(value = "exist", required = false) String exist, @AuthenticationPrincipal User principal,
                                        @ModelAttribute User user, BindingResult result) {
        ModelAndView model = new ModelAndView();

        if(principal != null) {
            model.setViewName("redirect:books");
            return model;
        }

        if(exist != null) {
            model.addObject("error", "Sorry, such user already exists");
        }
        model.setViewName("user/register");
        return model;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String postRegisterPage(@ModelAttribute @Valid User user, BindingResult result) {

        if(userService.findUserByLogin(user.getLogin()) != null) {
            ObjectError exist = new ObjectError("exist", "Sorry, such user already exists");
            result.addError(exist);
        }

        if (result.hasErrors()) {
            return "user/register";
        } else {
            userService.addUser(user);
            return "redirect:login?register";
        }
    }

    // Spring Security see this :
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView getLoginForm(
            @RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "logout", required = false) String logout,
            @RequestParam(value = "register", required = false) String register,
            @AuthenticationPrincipal User user) {

        ModelAndView model = new ModelAndView();

        if(user != null) {
            model.setViewName("redirect:books");
            return model;
        }

        if (error != null) {
            model.addObject("error", "Invalid username or password!");
        }

        if (logout != null) {
            model.addObject("msg", "You've been logged out successfully.");
        } else if (register != null) {
            model.addObject("msg", "Registered successfully, now you can login");
        }

        model.setViewName("user/login");
        return model;
    }
}
